package com.dyz.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import com.dyz.model.AFaktura;
import com.dyz.model.Koszty;
import com.dyz.model.Model;
import com.dyz.model.Przychod;
import com.dyz.util.InvoiceFields;
import com.dyz.util.UserAction;
import com.dyz.view.FileExplorer;
import com.dyz.view.Widok;
import com.dyz.view.WidokRoboczy;

public class Controller {

	Model m;
	Widok w = Widok.getSingletonInstance();
	WidokRoboczy wr;
	String nazwaPliku;

	public Controller(Model m, WidokRoboczy wr) {
		this.m = m;
		this.wr = wr;
		this.wr.addMemuListener(new MenuListener());
		listeners();
	}

	private void listeners() {

		w.addComboListener(new ComboboxListener());
		w.addButtonsListener(new ButtonsListener());
	}

//	public void newView(String s) {
//		if (flag == false) {
//			w.elements();
//			listeners();
//			flag = true;
//
//		}
//
//		w.setWindowName(s);
//	}

	class MenuListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			String s = e.getActionCommand();
			FileExplorer fileExplorer = new FileExplorer();
			String savePath = null;

			switch (s) {
			case UserAction.NEW:
				fileExplorer.getSaveExplorer(w);
				
				String directory = fileExplorer.getDirectory();
				String file = fileExplorer.getFile();

//				if (!savePath.equals("nullnull")) {
//					System.out.println(savePath);
//					m.makeFile(savePath, w);
//				//	newView(m.getBierzacaNazwa());
//					w.clearCbx();
//					updateCombo();
//				}
				
				if(directory == null || file == null) {
					break;
				} else {
					savePath = directory + file;
					m.makeFile(savePath, w);
					wr.addPanel(w.getPanel());
					w.clearCbx();
					updateCombo();
					
				}

				break;
			case UserAction.OPEN:

				fileExplorer.getOpenExplorer(w);

				String openPath = fileExplorer.getDirectory()
						+ fileExplorer.getFile();

				if (!openPath.equals("nullnull")) {
					System.out.println(openPath);
					m.loadFile(openPath);
					//newView(m.getBierzacaNazwa());
					wr.addPanel(w.getPanel());
				//	listeners();
					w.clearCbx();
					updateCombo();
				}

				break;
			case UserAction.SAVE:
				break;
			case UserAction.CLOSE:
				w.dispose();
			}
		}

	}

	class ButtonsListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			AFaktura fakt;

			String data = w.getTxt()[0];
			System.out.println(data);
			String numer = w.getTxt()[1];
			String firma = w.getTxt()[2];
			String adres = w.getTxt()[3];
			String opis = w.getTxt()[4];
			int vat = w.getVat();

			float k1 = new Float(w.getTxt()[5]);
			float k2 = new Float(w.getTxt()[6]);

			if (!m.containsFirm(firma)) {

				m.newPair(firma, adres);
				w.addToCbx1(firma);
				w.addToCbx2(adres);
			}

			else if (!m.containsAdress(adres)) {

				m.newPair(firma, adres);
				w.addToCbx2(adres);

			}

			if (w.radioState().equals("koszt")) {

				fakt = new Koszty(numer, data, firma, adres, opis, vat, k1, k2);
				// m.dodajFakture(fakt);
				m.save(fakt);
				w.wyczysc();
				System.out.println(fakt);

			} else if (w.radioState().equals("przychod")) {
				fakt = new Przychod(numer, data, firma, adres, opis, vat, k1,
						k2);
				// m.dodajFakture(fakt);
				m.save(fakt);
				w.wyczysc();
				System.out.println(fakt);

			} else {
				// lblNewLabel.setText("zaznacz rodzaj faktury");
				System.out.println("zaznacz rodzja fatk");
			}

		}

	}

	class ComboboxListener implements ActionListener {

		@SuppressWarnings("unchecked")
		public void actionPerformed(ActionEvent e) {

			JComboBox<String> cb;
			cb = (JComboBox<String>) e.getSource();

			switch (cb.getActionCommand()) {
			case "firma":

				String s = (String) cb.getSelectedItem();
				w.setTxt3(s);
				w.setTxt4(m.getAdres(s));

				break;

			case "adres":

				String s1 = (String) cb.getSelectedItem();
				w.setTxt4(s1);

				break;

			case "miesiac":
				String miesiac;

				switch (miesiac = (String) cb.getSelectedItem()) {

				case InvoiceFields.Months.JANUARY:
					m.wydrukuj(1, miesiac);
					break;
				case InvoiceFields.Months.FEBRUARY:
					m.wydrukuj(2, miesiac);
					break;
				case InvoiceFields.Months.MARCH:
					m.wydrukuj(3, miesiac);
					break;
				case InvoiceFields.Months.APRIL:
					m.wydrukuj(4, miesiac);
					break;
				case InvoiceFields.Months.MAY:
					m.wydrukuj(5, miesiac);
					break;
				case InvoiceFields.Months.JUNE:
					m.wydrukuj(6, miesiac);
					break;
				case InvoiceFields.Months.JULY:
					m.wydrukuj(7, miesiac);
					break;
				case InvoiceFields.Months.AUGUST:
					m.wydrukuj(8, miesiac);
					break;
				case InvoiceFields.Months.SEPTEMBER:
					m.wydrukuj(9, miesiac);
					break;
				case InvoiceFields.Months.OCTOBER:
					m.wydrukuj(10, miesiac);
					break;
				case InvoiceFields.Months.NOVEMBER:
					m.wydrukuj(11, miesiac);
					break;
				case InvoiceFields.Months.DECEMBER:
					m.wydrukuj(12, miesiac);
					break;

				}

				cb.setSelectedIndex(0);
				break;

			}

		}

	}

	public void updateCombo() {

		for (String s : m.getFirms())
			w.addToCbx1(s);
		for (String s : m.getAdress())
			w.addToCbx2(s);
	}

}
